﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using ARS.Control.Custom.Entity.Fields;

namespace ARS.Control.Custom.Entity.Forms
{
    public class ChoicesForm : Form
    {
        /// <summary>
        /// 获取选项列表
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
//        public static Dictionary<string, string> GetChoicesList(Fields.ChoicesField field)
//        {
//            Dictionary<string, string> dict = new Dictionary<string, string>();
//            string[] list = field.Choices.Split(',');
//            foreach (string str in list)
//            {
//                string[] vt = str.Split('|');
//                dict.Add(vt[0], vt[1]);
//            }
//            return dict;
//        }

        public  ARS.PublicTool.DataHandle.ChoicesHelper ChoicesHelper=new PublicTool.DataHandle.ChoicesHelper();

        protected override void RenderContents(System.Web.UI.HtmlTextWriter output)
        {
            base.RenderContents(output);
            Fields.ChoicesField field = (Fields.ChoicesField) base.Field;
            StringBuilder sb_select = new StringBuilder();
            if (field.IsAllowBlank == false)
            {
                sb_select.Append(string.Format("<select name=\"{0}\" reg='.+' tip='必须选择'>", field.Html_Control_Name));
            }
            else
            {
                sb_select.Append(string.Format("<select name=\"{0}\">", field.Html_Control_Name));
            }

            Dictionary<string, string> dict = ChoicesHelper.GetListFromChoices(field.Choices);
            foreach (var item in dict)
            {
                if (field.Value != null)
                {
                    if (field.Value.ToString() == item.Key)
                    {
                        sb_select.Append(string.Format("<option value=\"{0}\"  selected=\"selected\">{1}</option>", item.Key, item.Value));
                        continue;
                    }
                }
                sb_select.Append(string.Format("<option value=\"{0}\">{1}</option>", item.Key, item.Value));
            }

            //string[] list = field.Choices.Split(',');

            //foreach(string str in list)
            //{
            //    string[] vt = str.Split('|');
            //    if (field.Value != null)
            //    {
            //        if (field.Value.ToString() == vt[0])
            //        {
            //            sb_select.Append(string.Format("<option value=\"{0}\"  selected=\"selected\">{1}</option>", vt[0], vt[1]));
            //            continue;
            //        }
            //    }              
            //    sb_select.Append(string.Format("<option value=\"{0}\">{1}</option>", vt[0], vt[1]));
            //}

            sb_select.Append("</select>");
            output.Write(string.Format(base.GetTrHtml(), sb_select));
        }

        public override object GetInputValue()
        {

            ChoicesField choicesField = (ChoicesField) this.Field;
            string value = HttpContext.Current.Request.Form[choicesField.Html_Control_Name];
            return value;
//            if (string.IsNullOrEmpty(value))
//            {
//                return null;
//            }
//            else
//            {
//                return ChoicesHelper.GetValueFromChoices(choicesField.Choices, value);                
//            }
            //return base.GetInputValue();
        }
    }
}
